import numpy as np
from numpy.core import (array, asarray, zeros, swapaxes, shape, conjugate,
                        take, sqrt)



def _cook_nd_args(a, s=None, axes=None, invreal=0):
    if s is None:
        shapeless = 1
        if axes is None:
            s = list(a.shape)
        else:
            s = take(a.shape, axes)
    else:
        shapeless = 0
    s = list(s)
    if axes is None:
        axes = list(range(-len(s), 0))
    if len(s) != len(axes):
        raise ValueError("Shape and axes have different lengths.")
    if invreal and shapeless:
        s[-1] = (a.shape[axes[-1]] - 1) * 2
    return s, axes


def _raw_fftnd(a, s=None, axes=None, function=np.fft.fft, norm=None):
    a = asarray(a)
    print('asarray')
    print(a)
    s, axes = _cook_nd_args(a, s, axes)

    print('_cook_nd_args')
    print(s)
    print(axes)
    itl = list(range(len(axes)))
    print('itl')
    print(itl)
    itl.reverse()
    for ii in itl:
        print('itl iterate: ' + str(ii) + ' times' )
        print(s[ii])
        print(axes[ii]) 
        a = np.fft.fft(a, n=s[ii], axis=axes[ii], norm=norm)
        # print(a)
    return a




np.set_printoptions(precision=40)

# a = np.array([-4.78704,7.29176,3.12263,-8.72102,6.54695,0.408178,-4.7643,8.49932,4.50451,4.82586,-1.61296,-6.11842,6.81645,-4.72776,9.40993,9.21799,6.30043,0.356149,9.26544,0.188188,6.08789,-2.20969,-3.79914,4.79643,6.3003,5.77904,8.94648,-5.96991,3.8167,-0.0843859,-4.35136,5.73947,-7.23059,-5.68433,-7.1156,6.5942,-5.82258,8.67612,2.28971,-5.46597,-6.2194,-2.35787,-4.4362,6.91762,3.8671,2.11413,-1.71618,-4.32053,-8.21891,9.02178,-4.26638,6.41182,8.89988,-9.22317,9.2729,-3.42687,-2.20216,8.36345,-3.33361,-0.582502,8.56064,-6.92284,-7.1699,7.43537,-5.4205,4.00934,5.71638,3.46468,5.52623,-5.09222,5.47945,4.91742,9.52414,5.93015,1.37192,9.75395,2.89023,-2.04077,4.04222,3.16381,4.23908,2.74418,-7.42094,1.31623,2.46494,8.66823,1.67354,9.28316,3.56826,-6.13187,9.67387,8.8831,5.74408,8.51188,-4.05938,2.61206,9.96027,-1.04645,-8.04589,9.46737,-7.64729,-9.73076,3.79234,1.38356,5.56501,-8.32773,-9.79861,-6.05619,-4.16369,8.51229,9.102,9.97116,-3.37822,-4.85615,2.9658,1.38999,5.21447,7.64248,5.8366,6.93744,-3.39166,-8.33209,6.82778,7.35694,-1.2894,2.03941,6.29718,9.58047,4.02015,3.95993,-8.56762,5.8279,7.92214,8.71179,4.16833,0.941185,-5.87077,7.72893,-8.76613,1.47286,4.25104,-5.01745,-8.81623,4.67624,-3.64313,-8.14923,-0.634002,0.288464,-1.79703,-1.60912])
a = np.array([-4.78704, 7.29176, 3.12263,-8.72102, 6.54695,  0,
  0.408178  -4.7643,  8.49932, 4.50451, 4.82586, 0,
 -1.61296,-6.11842, 6.81645,-4.72776, 9.40993, 0,
  9.21799, 6.30043, 0.356149,9.26544, 0.188188,0,
  6.08789,-2.20969,-3.79914, 4.79643, 6.3003,  0,
  5.77904, 8.94648,-5.96991, 3.8167, -0.0843859,0,
 -4.35136, 5.73947,-7.23059,-5.68433,-7.1156,  0,
  6.5942, -5.82258, 8.67612, 2.28971,-5.46597, 0,
 -6.2194, -2.35787,-4.4362,  6.91762, 3.8671,  0,
  2.11413,-1.71618,-4.32053,-8.21891, 9.02178, 0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
 -4.26638, 6.41182, 8.89988,-9.22317, 9.2729,  0,
 -3.42687,-2.20216, 8.36345,-3.33361,-0.582502,0,
  8.56064,-6.92284,-7.1699,  7.43537,-5.4205,  0,
  4.00934, 5.71638, 3.46468, 5.52623,-5.09222, 0,
  5.47945, 4.91742, 9.52414, 5.93015, 1.37192, 0,
  9.75395, 2.89023,-2.04077, 4.04222, 3.16381, 0,
  4.23908, 2.74418,-7.42094, 1.31623, 2.46494, 0,
  8.66823, 1.67354, 9.28316, 3.56826,-6.13187, 0,
  9.67387, 8.8831,  5.74408, 8.51188,-4.05938, 0,
  2.61206, 9.96027,-1.04645,-8.04589, 9.46737, 0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
 -7.64729,-9.73076, 3.79234, 1.38356, 5.56501, 0,
 -8.32773,-9.79861,-6.05619,-4.16369, 8.51229, 0,
  9.102,,9.97116,-3.37822,-4.85615, 2.9658,  0,
  1.38999, 5.21447, 7.64248, 5.8366,  6.93744, 0,
 -3.39166,-8.33209, 6.82778, 7.35694,-1.2894,  0,
  2.03941, 6.29718, 9.58047, 4.02015, 3.95993, 0,
 -8.56762, 5.8279,  7.92214, 8.71179, 4.16833, 0,
  0.941185  -5.87077, 7.72893,-8.76613, 1.47286, 0,
  4.25104,-5.01745,-8.81623, 4.67624,-3.64313, 0,
 -8.14923,-0.634002,0.288464  -1.79703,-1.60912, 0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0,
  0,0,0,0,0,0])

a = a.reshape(3,10,5)

d_length = a.shape[0] 
h_length = a.shape[1] 
w_length = a.shape[2] 



print('shape')
print(slice(0,10))
print(a.shape[-2])

# print(a)
# print(swapaxes(a, -3, -1))
# axis 代表实际的长度
# n 代表给定的长度
# print(np.fft.fftn(a))
print('norm')
# print(np.fft.fft(a, n=5, axis=-1, norm=norm))
print('without norm')
# print(np.fft.fft([-4.78704,7.29176,3.12263,-8.72102,6.54695]))
print(_raw_fftnd(a))



a_tmp_d = np.zeros(d_length * h_length * w_length).reshape(3,10,5)

for i in range(d_length):
    a_tmp_d[i,:,:] = np.fft.fft2(a[i,:,:])

a_tmp_h = np.zeros(d_length * h_length * w_length).reshape(3,10,5)
for i in range(h_length):
    a_tmp_h[:,i,:] = np.fft.fft2(a_tmp_d[:,i,:])

a_tmp_w = np.zeros(d_length * h_length * w_length).reshape(3,10,5)
for i in range(w_length):
    a_tmp_w[:,:,i] = np.fft.fft2(a_tmp_h[:,:,i])

# print('a_tmp_w')
# print(a_tmp_w)






